package com.kool.kadmin.client;

import com.alibaba.fastjson.JSON;
import com.kool.kadmin.client.vo.MetricsVo;
import com.kool.kadmin.common.constant.ErrorCode;
import com.kool.kadmin.common.exception.AppException;
import com.kool.kadmin.common.util.HttpUtil;
import com.kool.kadmin.common.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author luyu
 * @date 2021-04-19 22:15
 */
@Component
@Slf4j
public class KmetricsClient {
    @Value("${kmetrics.url:null}")
    private String urlPrefix;

    /**
     * 查询平台实时指标
     *
     * @return
     */
    public MetricsVo getGlobalCnt() {
        String url = urlPrefix + "/kmetrics/getGlobalCnt";
        try {
            String response = HttpUtil.doPost(url, "", null, 5000, 5000);
            String data = ResultUtil.unpackData(response);
            return JSON.parseObject(data, MetricsVo.class);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new AppException(ErrorCode.CLIENT_ERROR, "kmetrics", e.getMessage());
        }
    }
}
